﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Twillo.Core.Web
{
    public static class Proxy
    {

        public static WebProxy WebProxy
        {
            get
            {
                if (_proxy == null) { _proxy = Create(); }
                return _proxy;
            }
        }

        private static WebProxy _proxy = null;

        private static WebProxy Create()
        {
            IWebProxy proxy = HttpWebRequest.GetSystemWebProxy();
            if (proxy == null) { proxy = HttpWebRequest.DefaultWebProxy; }

            if (proxy == null) { return null; }

            WebProxy value = new WebProxy(proxy.GetProxy(new Uri("http://www.twitter.com")));

            if (!string.IsNullOrEmpty(Settings.SettingsManager.ProxyUsername))
            {
                proxy.Credentials = Settings.SettingsManager.ProxyCredentials;
            }
            else
            {
                while (AuthenticationRequired(proxy))
                {
                    Forms.frmCredentials frm = new Forms.frmCredentials();
                    frm.Caption = "Proxy Authentication Required:";
                    if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        proxy.Credentials = frm.Credentials;
                        value.Credentials = proxy.Credentials;
                        if (frm.RememberCredentials)
                        {
                            Settings.SettingsManager.ProxyUsername = frm.Username;
                            Settings.SettingsManager.ProxyPassword = frm.Password;
                        }
                    }
                    else
                    {
                        proxy = null;
                        value = null;
                        break;
                    }
                }
            }

            if (proxy != null) { value.Credentials = proxy.Credentials; }            
            return value;
        }

        private static bool AuthenticationRequired(IWebProxy proxy)
        {
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            try
            {
                request = HttpWebRequest.Create("http://www.twitter.com") as HttpWebRequest;
                request.Proxy = proxy;
                if (!string.IsNullOrEmpty(Settings.SettingsManager.ProxyUsername))
                {
                    request.Proxy.Credentials = Settings.SettingsManager.ProxyCredentials;
                }
                response = request.GetResponse() as HttpWebResponse;

                response.Close();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return false;
                }

                return true;
            }
            catch
            {
                if (response != null) { response.Close(); }
                return true;
            }

        }
    }
}
